package cn.frh.basic.service.impl;

import cn.frh.basic.mapper.BaseMapper;
import cn.frh.basic.query.BaseQuery;
import cn.frh.basic.service.IBaseService;
import cn.frh.basic.util.PageList;
import cn.frh.log.annotation.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T>{

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    @Log(value = "添加")
    public void add(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    @Log(value = "修改")
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    @Log(value = "删除一个")
    public void remove(Long id) {
        baseMapper.delete(id);
    }

    @Override
    @Log(value = "查询一个")
    public T queryById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    @Log(value = "查询所有")
    public List<T> queryAll() {
        return baseMapper.loadAll();
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        //先查出总条数
        Long total = baseMapper.queryCount(query);
        //再查询出当前页数据
        List<T> rows = baseMapper.queryData(query);
        return new PageList<>(total,rows);
    }

    @Override
    @Transactional
    @Log(value = "批量删除")
    public void batchRemove(List<Long> ids) {
        baseMapper.batchRemove(ids);
    }
}
